package cn.bigfire.wedesk.action;

import cn.bigfire.wedesk.annotation.CMD;
import cn.bigfire.wedesk.bean.Wedesk;
import cn.bigfire.wedesk.gui.ShowFrame;
import cn.bigfire.wedesk.store.Stores;
import cn.bigfire.wedesk.util.ImgUtil;
import cn.bigfire.wedesk.util.KeyMapping;
import cn.bigfire.wedesk.util.RLEncoding;
import cn.hutool.core.swing.RobotUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;

import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.image.BufferedImage;

public class ClientAction {

    @CMD(Wedesk.Cmd.CMD_PING)
    public void ping(Wedesk.Err err) {
        if (err != null) {
            System.err.println(JSON.toJSONString(err));
        } else {
            System.out.println("收到ping请求");
        }
    }

    @CMD(Wedesk.Cmd.CMD_PC_INFO)
    public void pcInfo(Wedesk.Err err) {
        if (err != null) {
            System.err.println(JSON.toJSONString(err));
        } else {
            System.out.println("Pc端数据上报成功");
        }
    }

    @CMD(Wedesk.Cmd.CMD_REGISTER)
    public void register(Wedesk.RegisterRes registerRes) {
        System.out.println("register:" + JSON.toJSONString(registerRes));
    }

    //客户端发对象，服务器自动解构注入
    @CMD(Wedesk.Cmd.CMD_LOGIN)
    public void login(Wedesk.LoginRes loginRes) {
        Stores.updateDevices(loginRes.getPcOnlineMap());
        System.out.println("login:");
        System.out.println(JSONUtil.toJsonPrettyStr(loginRes));
    }


    @CMD(Wedesk.Cmd.CMD_CTRL_FILE_LIST)
    public void ctrlList(Wedesk.CtrlMyListRes ctrlListRes) {
        Stores.updateDevices(ctrlListRes.getPcOnlineMap());
        System.out.println("ctrlList:");
        System.out.println(JSONUtil.toJsonPrettyStr(ctrlListRes));
    }


    @CMD(Wedesk.Cmd.CMD_CTRL_OPEN)
    public void ctrlOpen(Wedesk.Ctrl ctrlOpenReq) {
        System.out.println("ctrlOpenReq:" + JSON.toJSONString(ctrlOpenReq));
        Stores.addCtrlOpen(ctrlOpenReq);
    }

    @CMD(Wedesk.Cmd.CMD_CTRL_CLOSE)
    public void ctrlClose(Wedesk.Ctrl ctrlExecReq) {
        System.out.println("ctrlExec:" + JSON.toJSONString(ctrlExecReq));
        Stores.removeCtrlOpen(ctrlExecReq);
    }


    @CMD(Wedesk.Cmd.CMD_SCREENSHOT_DATA)
    public void imgData(Wedesk.ScreensHot screensHot) {
        try {
            int width = screensHot.getWidth();
            int height = screensHot.getHeight();

//            int width =800;
//            int height = 600;
            int[] decompress = new RLEncoding().decompress(width, height, screensHot.getData());

            width =800;
            height = 600;
            //1440 900

            //800 600


            BufferedImage screen = ShowFrame.canvas.getScreen();
            if (screen == null) {
                screen = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
                screen.setRGB(0, 0, width, height, decompress, 0, width);
            } else {
                int[] bimap = screen.getRGB(0, 0, width, height, null, 0, width);
                for (int i = 0; i < bimap.length; i++) {
                    if (decompress[i] != 0) {
                        bimap[i] = decompress[i];
                    }
                }
                screen.setRGB(0, 0, width, height, bimap, 0, width);
            }

            ShowFrame.canvas.setScreen(screen);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

    @CMD(Wedesk.Cmd.CMD_CTRL_MOUSE)
    public void mouseEvent(Wedesk.MouseEvent mouseEvent) {
        if ("wheel".equals(mouseEvent.getType())) {
            int wheelAmt = 0;
            if (mouseEvent.getDeltaY() > 0) {
                wheelAmt = 1;
            } else if (mouseEvent.getDeltaY() < 0) {
                wheelAmt = -1;
            }
            RobotUtil.mouseWheel(wheelAmt);
        } else if ("mousedown".equals(mouseEvent.getType())) {
            int key = mouseKey(mouseEvent.getWhich());
            System.out.println("which:" + mouseEvent.getWhich() + ",key:" + key);
            RobotUtil.mouseMove(mouseEvent.getX(), mouseEvent.getY());//移动到对应的位置上点击
            RobotUtil.getRobot().mousePress(key);
        } else if ("mousemove".equals(mouseEvent.getType())) {
            RobotUtil.mouseMove(mouseEvent.getX(), mouseEvent.getY());//移动鼠标
        } else if ("mouseup".equals(mouseEvent.getType())) {
            int key = mouseKey(mouseEvent.getWhich());
            RobotUtil.mouseMove(mouseEvent.getX(), mouseEvent.getY());//移动到对应的位置上释放鼠标
            RobotUtil.getRobot().mouseRelease(key);
        } else {
            System.err.println("鼠标事件_未知的type:" + JSON.toJSONString(mouseEvent));
        }
    }

    // 鼠标which 转 鼠标key
    public static int mouseKey(int which) {
        int key = InputEvent.BUTTON1_MASK;//鼠标左键
        if (which == 2) {
            key = InputEvent.BUTTON2_MASK;//鼠标中键
        } else if (which == 3) {
            key = InputEvent.BUTTON3_MASK;//鼠标右键
        }
        return key;
    }

    @CMD(Wedesk.Cmd.CMD_CTRL_KEY_BOARD)
    public void keyboardEvent(Wedesk.KeyboardEvent keyboardEvent) {
        int keyCode = keyboardEvent.getKeyCode();
        int key = KeyMapping.convert(keyboardEvent.getKeyCode());//转换key
        if ("keydown".equals(keyboardEvent.getType())) {
            System.out.println("keyCode:" + keyCode + ",key:" + key);
            RobotUtil.getRobot().keyPress(key);
        } else if ("keyup".equals(keyboardEvent.getType())) {
            RobotUtil.getRobot().keyRelease(key);
        } else {
            System.err.println("键盘事件_未知的type:" + JSON.toJSONString(keyboardEvent));
        }
    }

}
